package com.codingapi.core.framework.utils;

import com.codingapi.netflix.framework.Constants;
import com.codingapi.netflix.framework.exception.ServerFeignException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 侯存路
 * @date 2018/4/10
 * @company codingApi
 * @description
 */
public class VerificationUtils {
    /**
     * 打印日志信息
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(VerificationUtils.class);

    /**
     * 检查参数有效性
     *
     * @param exception
     * @param o         类型为object
     * @throws ServerFeignException
     */
    public static void check(String exception, Object... o) throws ServerFeignException {
        for (int i = 0; i < o.length; i++) {
            if (o.clone()[i] == null) {
                LOGGER.error("参数" + o.clone()[i] + "发生异常" + exception + "错误代码{}" + Constants.GLOBAL_EXCEPTION_DEFAULT_CODE);
                throw new ServerFeignException(Constants.GLOBAL_EXCEPTION_DEFAULT_CODE, exception);
            }
        }
    }

    /**
     * 检验参数的有效性
     *
     * @param exception
     * @param o         字符串可变参数
     * @throws ServerFeignException
     */
    public static void check(String exception, String... o) throws ServerFeignException {
        for (int i = 0; i < o.length; i++) {
            if (StringUtils.isEmpty(o.clone()[i])) {
                LOGGER.error("参数" + o.clone()[i] + "发生异常" + exception + "错误代码{}" + Constants.GLOBAL_EXCEPTION_DEFAULT_CODE);
                throw new ServerFeignException(Constants.GLOBAL_EXCEPTION_DEFAULT_CODE, exception);
            }
        }
    }

    public static void check(String exception, boolean o) throws ServerFeignException {
        if (o != true) {
            LOGGER.error("发生异常" + exception + "错误代码{}" + Constants.GLOBAL_EXCEPTION_DEFAULT_CODE);
            throw new ServerFeignException(Constants.GLOBAL_EXCEPTION_DEFAULT_CODE, exception);
        }
    }

    public static void check(String exception, int o) throws ServerFeignException {
        if (o == 0) {
            LOGGER.error("发生异常" + exception + "错误代码{}" + Constants.GLOBAL_EXCEPTION_DEFAULT_CODE);
            throw new ServerFeignException(Constants.GLOBAL_EXCEPTION_DEFAULT_CODE, exception);
        }
    }
}
